﻿; (function ($) {
    var getFileIcon = function (type) {
        switch (type) {
            case '.wave': case '.midi': case '.cda': case '.mp3': case '.wma': case 'ape': case '.flac': case '.aac':
                return 'icon-yunpanlogo-';
                break;
            case '.png': case '.jpg': case '.jpeg': case '.bmp': case '.gif': case '.webp': case '.psd': case '.svg': case '.tiff':
                return 'icon-yunpanlogo-5';
                break;
            case '.wmv': case '.avi': case '.dat': case '.asf': case '.mpeg': case '.mpg': case '.rm': case '.rmvb': case '.ram': case '.flv': case '.mp4': case '.3gp': case '.mov': case '.divx': case '.dv': case '.vob': case '.mkv': case '.qt': case '.cpk': case '.fli': case '.flc': case '.f4v': case '.m4v': case '.mod': case '.m2t': case '.swf': case '.webm': case '.mts': case '.m2ts':
                return 'icon-mp4';
                break;
            case '.html': case '.htm': case '.shtml': case '.shtm':
                return 'icon-yunpanlogo-1';
                break;
            case '.txt':
                return 'icon-yunpanlogo-2';
                break;
            case '.iso': case '.isz': case '.esd': case '.wim': case '.gho':
                return 'icon-yunpanlogo-3';
                break;
            case '.exe': case '.msi': case '.com': case '.bat': case '.cmd':
                return 'icon-yunpanlogo-4';
                break;
            case '.xls': case '.xlsx':
                return 'icon-xls';
                break;
            case '.doc': case '.docx':
                return 'icon-dotm';
                break;
            case '.ppt': case '.pptx':
                return 'icon-pptx';
                break;
            case '.rar': case '.zip': case '.cab': case '.jar': case '.ace': case '.7z': case '.tar': case '.gz':
                return 'icon-rar';
                break;
            case '.pdf':
                return 'icon-pdf';
                break;
            default:
                return 'icon-doctype1';
        }
    }
    $.fn.fileupload = function (method) {
        if (methods[method]) {
            return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
        }
        else if (typeof method === 'object' || !method) {
            return methods.init.apply(this, arguments);
        }
        else {
            $.error('方法' + method + '不存在');
        }
    }
    $.fn.fileupload.defaults = {
        width: "",
        guid: utils.newGuid(),
        enable: true,
        allowDownload: true,
        allowDelete: true,
        allowUpload: true,
    };
    var methods = {
        init: function (_options) {
            return this.each(function () {
                var _this = $(this);
                console.log($.fn.fileupload.defaults.guid);
                console.log(_options.guid);
                var args = $.extend({}, $.fn.fileupload.defaults, _options);
                console.log(args.guid);
                _this.css({ width: args.width = "" ? "96%" : args.width, margin: "5px 0px 0px 0px", position: "relative"});
                if (args.enable == false || args.allowUpload == false)
                {
                    _this.append("<div id='div_upload'></div>");
                }
                else
                {
                    _this.append("<div id='div_upload' style='float:left;margin:0px 12px 5px 0px;width:5em;height:5em;line-height:5em;vertical-align:middle;border:1.5px solid #C9C9C9;border-radius:3px;text-align:center;'>        <button title='添加文件' type='button' class='layui-btn layui-btn-primary' id='btn_upload' style='width: 3.5em; height: 3.5em; padding:0px;text-align: center;border:0px;'>            <i class='layui-icon layui-icon-addition' style='font-size:3em;vertical-align: middle;margin:0px;'></i></button></div>");
                }
                //_this.append("<div id='divf' style='float:left;margin-left:10px;margin-bottom:10px;width:5em;height:5em;line-height:5em;vertical-align:middle;border:1.5px solid #C9C9C9;border-radius:3px;text-align:center;'><div style='position:absolute;width: 1em; height: 3.5em;'></div><div style='position:absolute;width: 3em; height: 3.5em;line-height:3.5em;margin-left:1em;vertical-align:middle;text-align:center;'>                <button id='btndownload' type='button' title='文件全名' class='layui-btn layui-btn-primary' style='padding:0px;text-align: center;border:0px;'><svg class='icon' aria-hidden='true' style='width:4.5em;height:4.5em;vertical-align:middle;fill:currentColor;overflow:hidden;margin:-0.7em 0em 0em -1.4em;'><use xlink:href='#" + getFileIcon('.pdf') + "'></use></svg></input></div><div style='position:absolute;width: 1em; height: 3.5em;margin-left:4em;'><button title='删除文件' id='btndelete' type='button' class='layui-btn layui-btn-radius layui-btn-danger' style='position: absolute; margin:-0.18em 0em 0px 0em;width: 1em;height: 1em;line-height:1em;padding:0px;text-align:center;vertical-align:middle;border-color:red;'><i class='layui-icon layui-icon-close' style='font-size:0.9em;margin:-0.3em 0px 0px 0px;color:white;'></i></button></div>            <div style='position:absolute;width: 5em; height: 1.5em;line-height:1.5em;margin-top:3.5em;vertical-align:middle;text-align:center;'>                <p style='overflow: hidden;text-overflow:ellipsis;white-space: nowrap;font-size:10px;'>文件名字</p></div></div>");
                com.ajax({
                    url: '/BD/File/GetFiles',
                    data: { guid: args.guid },
                    success: function (data) {
                        for (i = 0; i < data.length; i++) {
                            //var margin = (i == data.length-1 ? (first_margin ? 10 : 0) : 10);
                            var html = "<div id='divf" + data[i].ID + "' style='float:left;margin-right:12px;margin-bottom:5px;width:5em;height:5em;line-height:5em;vertical-align:middle;border:1.5px solid #C9C9C9;border-radius:3px;text-align:center;'><div style='position:absolute;width: 1em; height: 3.5em;'></div><div style='position:absolute;width: 3em; height: 3.5em;line-height:3.5em;margin-left:1em;vertical-align:middle;text-align:center;'>                <button id='btndownload" + data[i].ID + "' type='button' title='" + data[i].FileAllName + "' class='layui-btn layui-btn-primary' style='padding:0px;text-align: center;border:0px;'><svg class='icon' aria-hidden='true' style='width:4.5em;height:4.5em;vertical-align:middle;fill:currentColor;overflow:hidden;margin:-0.7em 0em 0em -1.4em;'><use xlink:href='#" + getFileIcon(data[i].Suffix) + "'></use></svg></input></div><div style='position:absolute;width: 1em; height: 3.5em;margin-left:4em;'><button title='删除文件' id='btndelete" + data[i].ID + "' type='button' class='layui-btn layui-btn-radius layui-btn-danger' style='position: absolute; margin:-0.18em 0em 0px 0em;width: 1em;height: 1em;line-height:1em;padding:0px;text-align:center;vertical-align:middle;border-color:red;'><i class='layui-icon layui-icon-close' style='font-size:0.9em;margin:-0.3em 0px 0px 0px;color:white;'></i></button></div>            <div style='position:absolute;width: 5em; height: 1.5em;line-height:1.5em;margin-top:3.5em;vertical-align:middle;text-align:center;'>                <p style='overflow: hidden;text-overflow:ellipsis;white-space: nowrap;font-size:10px;'>" + data[i].FileAllName + "</p></div></div>";
                            $('#div_upload').after(html);
                            if (args.enable == true && args.allowDownload == true) {
                                var tempid = data[i].ID;
                                $('#btndownload' + data[i].ID).click(function () {
                                    var url = "/BD/File/Download";
                                    var form = $("<form></form>").attr("action", url).attr("method", "post");
                                    form.append($("<input></input>").attr("type", "hidden").attr("name", "id").attr("value", tempid));
                                    form.appendTo('body').submit().remove();
                                });
                            }
                            else {
                                $('#btndownload' + data[i].ID).addClass('layui-btn-disabled');
                            }
                            if (args.enable == true && args.allowDelete == true) {
                                var tempid = data[i].ID;
                                $('#btndelete' + data[i].ID).click(function () {
                                    $.messager.confirm('确认对话框', '您确定删除文件吗？', function (r) {
                                        if (r) {
                                            com.ajax({
                                                url: '/BD/File/Delete',
                                                data: { ID: tempid },
                                                success: function (res) {
                                                    console.log('#divf' + tempid);
                                                    $('#divf' + tempid).remove();
                                                    $('#divf' + tempid).hide();
                                                    com.message("information", "删除成功");
                                                },
                                                error: function (data) {
                                                    com.message("information", "删除失败");
                                                }
                                            });
                                        }
                                    });
                                });
                            }
                            else {
                                $('#btndelete' + data[i].ID).hide();
                            }
                        }
                    },
                    error: function () {
                        com.message("information", "初始化失败");
                    }
                });
                layui.use('upload', function () {
                    var upload = layui.upload;
                    var uploadInst = upload.render({
                        elem: '#btn_upload',
                        url: '/BD/File/Upload',
                        accept: 'file',
                        multiple: true,//允许同时选择多个文件上传
                        number: 0,//允许同时上传的文件数，0为不限制
                        data: { GUID: args.guid },
                        done: function (data) {
                            if (data.res == "success") {
                                console.log(data.info.ID);
                                var html = "<div id='divf" + data.info.ID + "' style='float:left;margin-right:12px;margin-bottom:5px;width:5em;height:5em;line-height:5em;vertical-align:middle;border:1.5px solid #C9C9C9;border-radius:3px;text-align:center;'><div style='position:absolute;width: 1em; height: 3.5em;'></div><div style='position:absolute;width: 3em; height: 3.5em;line-height:3.5em;margin-left:1em;vertical-align:middle;text-align:center;'>                <button type='button' id='btndownload" + data.info.ID + "' title='" + data.info.FileAllName + "' class='layui-btn layui-btn-primary' style='padding:0px;text-align: center;border:0px;'><svg class='icon' aria-hidden='true' style='width:4.5em;height:4.5em;vertical-align:middle;fill:currentColor;overflow:hidden;margin:-0.7em 0em 0em -1.4em;'><use xlink:href='#" + getFileIcon(data.info.Suffix) + "'></use></svg></input></div><div style='position:absolute;width: 1em; height: 3.5em;margin-left:4em;'><button title='删除文件' id='btndelete" + data.info.ID + "' type='button' class='layui-btn layui-btn-radius layui-btn-danger' style='position: absolute; margin:-0.18em 0em 0px -0.1em;width: 1em;height: 1em;line-height:1em;padding:0px;text-align:center;vertical-align:middle;border-color:red;'><i class='layui-icon layui-icon-close' style='font-size:0.9em;margin:-0.3em 0px 0px 0px;color:white;'></i></button></div>            <div style='position:absolute;width: 5em; height: 1.5em;line-height:1.5em;margin-top:3.5em;vertical-align:middle;text-align:center;'>                <p style='overflow: hidden;text-overflow:ellipsis;white-space: nowrap;font-size:10px;'>" + data.info.FileAllName + "</p></div></div>";
                                $('#div_upload').after(html);
                                if (args.enable == true && args.allowDownload == true) {
                                    var tempid = data.info.ID;
                                    $('#btndownload' + data.info.ID).click(function () {
                                        var url = "/BD/File/Download";
                                        var form = $("<form></form>").attr("action", url).attr("method", "post");
                                        form.append($("<input></input>").attr("type", "hidden").attr("name", "id").attr("value", tempid));
                                        form.appendTo('body').submit().remove();
                                    });
                                }
                                else {
                                    $('#btndownload' + data.info.ID).addClass('layui-btn-disabled');
                                }
                                if (args.enable == true && args.allowDelete == true) {
                                    var tempid = data.info.ID;
                                    $('#btndelete' + data.info.ID).click(function () {
                                        $.messager.confirm('确认对话框', '您确定删除文件吗？', function (r) {
                                            if (r) {
                                                com.ajax({
                                                    url: '/BD/File/Delete',
                                                    data: { ID: tempid },
                                                    success: function (res) {
                                                        console.log('#divf' + tempid);
                                                        $('#divf' + tempid).remove();
                                                        $('#divf' + tempid).hide();
                                                        com.message("information", "删除成功");
                                                    },
                                                    error: function (data) {
                                                        com.message("information", "删除失败");
                                                    }
                                                });
                                            }
                                        });
                                    });
                                }
                                else {
                                    $('#btndelete' + data.info.ID).hide();
                                }
                            }
                            else {
                                com.message("information", data.msg);
                            }
                        },
                        error: function (ex) {

                        }
                    });
                });
                if (args.enable == false) {
                    $('button').attr("disabled", "disabled");
                    $('button').addClass('layui-btn-disabled');
                }
                if (args.allowUpload == false) {
                    $('#btn_upload').attr("disabled", "disabled");
                    $('#btn_upload').addClass('layui-btn-disabled');
                }
            })
        },
        getGuid: function (_options) {
            var args = $.extend({}, $.fn.fileupload.defaults, _options);
            return args.guid;
        },
    };
})(jQuery);